<template>
  <div class="my-container">
    <!-- 未登录 -->
    <div v-if="useState" class="header not-login">
      <div class="login-btn" @click="this.$router.push('/login')">
        <img class="mobile-img" src="../../assets/mobile.png" alt="" />
        <span class="text">注册 / 登录</span>
      </div>
    </div>
    <!-- 已登录 -->
    <div v-else class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image
            src="https://img.yzcdn.cn/vant/cat.jpeg"
            class="avatar"
            round
            fit="cover"
          >
          </van-image>
          <span class="name">{{ userInfo.userName|| '小天' }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <div class="data-status">
        <div class="class-item">
          <span class="count">{{ userInfo.art_count|| 8 }}</span>
          <span class="text">头条</span>
        </div>
        <div class="class-item">
          <span class="count">{{ userInfo.follow_count|| 10 }}</span>
          <span class="text">关注</span>
        </div>
        <div class="class-item">
          <span class="count">{{ userInfo.fans_count|| 10 }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="class-item">
          <span class="count">{{ userInfo.like_count|| 15 }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid :column-num="2" clickable>
      <van-grid-item icon="star-o" text="收藏" />
      <van-grid-item icon="clock-o" text="历史" />
    </van-grid>
    <!-- 消息通知与退出布局实现 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小天同学" is-link />
    <van-cell
      title="退出登录"
      v-if="!useState"
      @click="onLogout"
      class="logout-cell"
    />
  </div>
</template>

<script>
import { useStore } from "vuex";
import { computed, onMounted, toRefs,reactive } from "vue";
import { Dialog } from "vant";
import { getUserInfo } from "../../api/user";
function userLogout(store) {
  const onLogout = () => {
    Dialog({
      title: "确认退出吗?",
    }).then(() => {
      // console.log('确认执行')
      store.commit('setUser',null)
    }).catch(()=>{
      console.log('取消执行')
    });
  };
  return { onLogout };
}
function userLoadUserInfo(){
  const state = reactive({
    userInfo:{
      userName:'',
      art_count:0,
      follow_count:0,
      fans_count:0,
      like_count:0,
      photo:"",
    }
  });
  const store = useStore();
  const useState = computed(() => store.state.user);
  const loadUserInfo=()=>{
    if(useState.value !== null){
      getUserInfo(useState.value.data.id).then(res=>{
        state.userInfo = res.data;
      });
    }
  }
  onMounted(()=>{
    // 获取用户信息
    loadUserInfo();
  })
  return {
    store,
    useState,
    state
  }
}
export default {
  setup() {
    const {store,useState,state} = userLoadUserInfo()
    return {
      ...toRefs(state),
      useState,
      ...userLogout(store),
    };
  },
};
</script>

<style scoped>
.my-container .header {
  height: 480rpx;
  background-color: #2f86f6;
  background-size: cover;
}
.my-container .not-login {
  display: flex;
  justify-content: center;
  align-items: center;
}
.my-container .not-login .login-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.mobile-img {
  width: 132px;
  height: 132px;
  border-radius: 50%;
}
.text {
  font-size: 18px;
  color: #fff;
}

.user-info .base-info {
  height: 131px;
  padding: 70px 32px 23px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.user-info .left {
  display: flex;
  align-items: center;
}
.user-info .left .avatar {
  width: 132px;
  height: 132px;
  margin-right: 13px;
  border: 1px solid #fff;
}
.user-info .left .name {
  color: white;
  font-size: 18px;
}
.data-status {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.data-status .class-item {
  display: flex;
  flex-direction: column;
  margin-top: 32px;
  text-align: center;
  padding-bottom: 10px;
}
.data-status .class-item .count {
  color: #fff;
  font-size: 18px;
}
.nav-bargrid .van-grid-item__content .van-grid-item__icon {
  color: #eb5235;
}
.logout-cell {
  text-align: center;
  color: #d86262;
  height: 120px;
  margin-top: 9px;
}
</style>
